<?php
include(APPPATH.'/controllers/frontend.php');
class Order extends Frontend 
{
    
    function Order() 
    {
        parent::Frontend();
        $this->load->model("MImage");
    }
    
    function index()
    {
        $this->layout[MY_Controller::JS_FILES][] = 'home';
        $this->layout[MY_Controller::JS_VARS]['var_site_url'] = site_url();
        $this->layout[MY_Controller::JS_VARS]['msg_error'] = _("Error");
        $this->layout[MY_Controller::VIEW_FILE] = 'home';
        $this->data["font_list"] = &$this->MImage->font_list;
    }
    
    function ajax_preview() 
    {
        header('Content-type: application/json');
        $output = array(
            "success" => FALSE,
            "data"    => NULL
        );
        $order_texts = $this->input->post("order_texts");
        
        $width = 900;
        $height = 150;
        $text_size = 72;
        $text_color = array(255,0,255);
        $image_filename = "images/order_preview";
        
        if(!empty($order_texts))
        {
            $this->load->model("MImage");
            $this->MImage->create_image($width,$height,array(0,0,0));
            $total_width = floor($height/3);
            foreach($order_texts as $order_text) {
                if(isset($order_text["text"]) && $order_text["text"] != "" && isset($order_text["font"]) && $order_text["font"] != "")
                {
                    $text_width = $this->MImage->draw_text($total_width,floor($height*2/3),$order_text["text"],$text_size,strtoupper($order_text["font"]),$text_color);
                    $total_width += $text_width;
                    if($total_width >= $width) break;
                }
            }
            $watermark = $this->MImage->draw_text(0,90,"ProKute.Com ProKute.Com ProKute.Com ProKute.Com ProKute.Com",23,"PKF13",array(0,96,255),FALSE,110);
            $this->MImage->output_image($image_filename);
            $output["data"] = array(
                "image_url" => site_url($image_filename.".png?gen=".time())
            );
            $output["success"] = TRUE;
        }
        echo json_encode(&$output);
        return;
    }
}